import jpush
from celery import Celery
import json

# key和password由app端定义

# 测试推送
key = '46d6a308a9482b01847d9fe7'
password = '9a163cf20f7dc29a235a96c5'

# 正式推送
# key= 'bfbc1fdfe90d67f7497b84b6'
# password = 'fffde511eddd2598dd82c94e'

_jpush = jpush.JPush(key, password)

# 极光推送的rbmq
# app_push = Celery("jpushutils")
# app_push.conf.update(imports='jpushutils',BROKER_URL='amqp://', CELERY_IMPORTS='jpushutils', CELERY_DEFAULT_QUEUE="queue_startprocess")

# 流程的rbmq
# app_process = Celery("jpushutils")
# app_process.conf.update(imports='jpushutils',
#                         # BROKER_URL='amqp://',
#                         # BROKER_URL='amqp://admin:admin@172.16.18.122',
#                         BROKER_URL='amqp://admin:admin@59.110.217.232',
#                         CELERY_TASK_SERIALIZER='json',
#                         CELERY_RESULT_SERIALIZER='json',
#                         CELERY_ENABLE_UTC=True,
#                         CELERY_IMPORTS='jpushutils',
#                         CELERY_ACCEPT_CONTENT=['json'],
#                         CELERY_DEFAULT_QUEUE="queue_startprocess"
#                         )

# @app_push.task(name="J_Push")
def j_push(message, user_alias):
    '''
    调用极光api推送消息
    :message:       消息参数
    :user_alias:    用户user_id列表
    '''
    push = _jpush.create_push()
    alias = {"alias": user_alias}
    push.audience = jpush.audience(
        alias
    )
    push.notification = jpush.notification(alert=message)
    push.platform = jpush.all_
    try:
        response = push.send()
    except jpush.common.Unauthorized:
        raise jpush.common.Unauthorized("Unauthorized")
    except jpush.common.APIConnectionException:
        raise jpush.common.APIConnectionException("conn")
    except jpush.common.JPushFailure:
        print ("JPushFailure")
    except:
        print ("Exception")
    return "推送成功"


# @app_push.task(name="J_Push")
# def j_push(message, user_alias):
#     '''
#     调用极光api推送消息
#     :message:       消息参数
#     :user_alias:    用户user_id列表
#     '''
#     dict = {
#         "message": message,
#         "user_alias": user_alias
#     }
#     dict_json = json.dumps(dict)
#     return dict_json


# @app_process.task(name="Process")
# def process(userId, proessId, classId, processName):
#     '''
#     rabbitmq发送流程
#     :userId:        申请人id
#     :proessId:      processlist的id
#     :classId:       流程分类id
#     :processName:   流程名称
#     :return:        打包成json
#     '''
#     dict = {
#         "userId": userId,
#         "proessId": proessId,
#         "classId": classId,
#         "processName": processName
#     }
#     dict_json = json.dumps(dict)
#     return dict_json
